#include "sort.h"

static void __qsort(int* v, int start, int end) {
	int left = start;
	int right = end;
	int pivot = v[start];

	if (start < end) {
		while (left < right) {
			for (; left <= right && v[left] <= pivot; ++left);
			for (; left <= right && v[right] >= pivot; --right);
			if (left < right) {
				v[left] ^= v[right];
				v[right] ^= v[left];
				v[left] ^= v[right];
			}
		}

		v[start] = v[right];
		v[right] = pivot;
		__qsort(v, start, right-1);
		__qsort(v, right+1, end);
	}
}

void quicksort(int* v, int n) {
	__qsort(v, 0, n-1);
}

void bubblesort(int* v, int n) {
	int i;
	int sorted = 0;

	while (!sorted) {
		sorted = 1;
		for (i=0; i < n-1 && sorted; ++i) {
			if (v[i] > v[i+1]) {
				sorted = 0;
				v[i]   = v[i] ^ v[i+1];
				v[i+1] = v[i] ^ v[i+1];
				v[i]   = v[i] ^ v[i+1];
			}
		}
	}
}

